<% instances.each do |instance| -%>
resource "aws_instance" "<%= module_name_of(instance) %>" {
    ami                         = "<%= instance.image_id %>"
    availability_zone           = "<%= instance.placement.availability_zone %>"
    ebs_optimized               = <%= instance.ebs_optimized %>
    instance_type               = "<%= instance.instance_type %>"
<%- unless instance.placement.group_name.empty? -%>
    placement_group             = "<%= instance.placement.group_name %>"
<%- end -%>
    monitoring                  = <%= monitoring_state(instance) %>
    key_name                    = "<%= instance.key_name %>"
<%- if in_vpc?(instance) -%>
    subnet_id                   = "<%= instance.subnet_id %>"
    vpc_security_group_ids      = <%= instance.security_groups.map { |sg| sg.group_id }.inspect %>
<%- else -%>
    security_groups             = <%= instance.security_groups.map { |sg| sg.group_name }.inspect %>
<%- end -%>
    associate_public_ip_address = <%= associate_public_ip?(instance) %>
    private_ip                  = "<%= instance.private_ip_address %>"
<%- if instance.source_dest_check -%>
    source_dest_check           = <%= instance.source_dest_check %>
<%- end -%>

<% block_devices_of(instance).each do |block_device| -%>
<%- mapping = block_device_mapping_of(instance, block_device.volume_id) -%>
<%- if root_block_device?(mapping, instance) -%>
    root_block_device {
        volume_type           = "<%= block_device.volume_type %>"
        volume_size           = <%= block_device.size %>
        delete_on_termination = <%= mapping.ebs.delete_on_termination %>
<%- if block_device.volume_type == "io1" && block_device.iops -%>
        iops                  = <%= block_device.iops %>
<%- end -%>
    }
<%- else -%>
    ebs_block_device {
        device_name           = "<%= mapping.device_name %>"
        snapshot_id           = "<%= block_device.snapshot_id %>"
        volume_type           = "<%= block_device.volume_type %>"
        volume_size           = <%= block_device.size %>
        delete_on_termination = <%= mapping.ebs.delete_on_termination %>
<%- if block_device.volume_type == "io1" && block_device.iops -%>
        iops                  = <%= block_device.iops %>
<%- end -%>
    }
<% end -%>

<% end -%>
    tags {
<% instance.tags.each do |tag| -%>
        "<%= tag.key %>" = "<%= tag.value %>"
<% end -%>
    }
}

<% end -%>
